package isnork.g2;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import isnork.g2.utility.Utility;
import isnork.sim.BoardPanel;
import isnork.sim.GameObject;
import isnork.sim.Observation;
import isnork.sim.SeaLifePrototype;
import isnork.sim.GameObject.Direction;


public class Explore implements Strategy {
	Double expHappDensity;
	double x,y;
	Point2D possPoint;
	int round = -1;
	Boolean nextRound = false;
	Direction direction_to_boat;
	public static double MAX = Double.MAX_VALUE;
 	Map<Integer,Point2D> toGoNext = new HashMap<Integer,Point2D>(); 
	private static final Point2D BOAT = new Point2D.Double(0.0, 0.0);
	int tick = 0;
	int finalTick = 10;
	
	Explore(G2Explorer diver)
	{
		this.expHappDensity = diver.expHappDensity;
		//System.out.printf("explore constructor called\n");
	}
	
	@Override	
	public Map<Direction, Double> process(G2Explorer diver) {
		// TODO Auto-generated method stub
		System.out.printf("Explore process started\n");
		Map<Direction,Double> utility_accumulatorExp = new HashMap<Direction,Double>();
		Point2D[] divers = new Point2D[9];
		//Direction [] nextDirection = new Direction [9];
		try{
			
			System.out.printf("inside the TRY block\n");
			
			/*if(tick > finalTick)
			{
				System.out.println("exploration over");
				//int id = (Math.abs(diver.getId())%8);
				
				//toGoNext.put(id, diver.location);
				
				for(Direction d: Direction.values())
					utility_accumulatorExp.put(d, 0.0);
		
				return utility_accumulatorExp;
			}
			*/
			if(round>2)
			{
				for(Direction d: Direction.values())
					utility_accumulatorExp.put(d,0.0);
						return utility_accumulatorExp;
			}
			
			if(nextRound && !diver.Visited.contains(new Point2D.Double((diver.location.getX()+Direction.values()[(Math.abs(diver.getId())+3)%8].getDx()),(diver.location.getY()+Direction.values()[(Math.abs(diver.getId())+3)%8].getDy()))))
			{
				if(new Point2D.Double((diver.location.getX()+Direction.values()[Math.abs(diver.getId())%8].getDx()),(diver.location.getY()+Direction.values()[Math.abs(diver.getId())%8].getDy())).distance(0, 0) > (diver.d-diver.r))
				{
					diver.Visited.clear();
					round++;
					direction_to_boat = Utility.RelativeDirection(diver.location, BOAT);
					utility_accumulatorExp.put(direction_to_boat, 2*expHappDensity);				
					
					System.out.println("id:" + diver.getId()+ "direction"+ direction_to_boat);
					return utility_accumulatorExp;
					
					/*utility_accumulatorExp.put(Utility.RelativeDirection(diver.location, new Point2D.Double(0,0)),10000.0);
					for(int i=0;i<7;i++)
						utility_accumulatorExp.put(Direction.allBut(Direction.values()[(Math.abs(diver.getId()))%7]).get(i),0.0);
				*/
				}
				else
					utility_accumulatorExp.put(Direction.values()[(Math.abs(diver.getId())+3)%8],2*expHappDensity);
				//for(int i=0;i<7;i++)
					//utility_accumulatorExp.put(Direction.allBut(Direction.values()[(Math.abs(diver.getId())+3)%7]).get(i),0.0);
				return utility_accumulatorExp;
			
			}
			
			else
			{	
				System.out.printf("explore.java : fourth if i.e. final else\n");
				utility_accumulatorExp.put(Direction.values()[Math.abs(diver.getId())%8],0.0);
			}
			
			if((new Point2D.Double((diver.location.getX()+Direction.values()[Math.abs(diver.getId())%8].getDx()),(diver.location.getY()+Direction.values()[Math.abs(diver.getId())%8].getDy())).distance(0, 0)> (diver.d - diver.r)) && !nextRound)
				{
					diver.Visited.clear();
					finalTick = tick;
					divers[Math.abs(diver.getId())%9] = diver.location;
					nextRound = true;
					System.out.println("tickcount"+tick);
				}
			
			if(!diver.Visited.contains(new Point2D.Double((diver.location.getX()+Direction.values()[Math.abs(diver.getId())%8].getDx()),(diver.location.getY()+Direction.values()[Math.abs(diver.getId())%8].getDy()))) && !nextRound)
			{
				utility_accumulatorExp.put(Direction.values()[Math.abs(diver.getId())%8], 2*expHappDensity);
				tick++;
			}
		
			
		}
		catch(Exception e){System.out.println(e);}
		//Direction direction_to_boat = Utility.directionTowardCreature(diver.location,BOAT);
		//System.out.printf("Direction to boat calculated %s\n",direction_to_boat);
		/*
		for(Direction d : Direction.values())
		{  
			possPoint = new Point2D.Double(x+d.getDx(),y+d.getDy()); 
			//System.out.println(possPoint.toString());
			if(!d.isDiag() && possPoint != diver.location && !diver.playerLocations.contains(possPoint) && diver.prevLocation != possPoint)
					utility_accumulatorExp.put(d,(expHappDensity));
			else
				utility_accumulatorExp.put(d,0.0);
		}
		*/
		return utility_accumulatorExp;
		
	}
}